# Copyright (c) 2011 CESNET
#
# LICENSE TERMS
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the Company nor the names of its contributors
#    may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# ALTERNATIVELY, provided that this notice is retained in full, this
# product may be distributed under the terms of the GNU General Public
# License (GPL) version 2 or later, in which case the provisions
# of the GPL apply INSTEAD OF those given above.
#
# This software is provided ``as is'', and any express or implied
# warranties, including, but not limited to, the implied warranties of
# merchantability and fitness for a particular purpose are disclaimed.
# In no event shall the company or contributors be liable for any
# direct, indirect, incidental, special, exemplary, or consequential
# damages (including, but not limited to, procurement of substitute
# goods or services; loss of use, data, or profits; or business
# interruption) however caused and on any theory of liability, whether
# in contract, strict liability, or tort (including negligence or
# otherwise) arising in any way out of the use of this software, even
# if advised of the possibility of such damage.
#

NAME = @PACKAGE_NAME@
VERSION = $(shell cut -f1 ./VERSION | tr -d '\n')
RELEASE = @RELEASE@

# Various configurable paths (remember to edit Makefile.in, not Makefile)
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@
BINDIR = $(shell eval echo $(bindir))
includedir = @includedir@
libdir =  @libdir@
mandir = @mandir@
sysconfdir = @sysconfdir@
dbusconfdir = @DBUSCONF@
dbusservices = @DBUSSERVICES@
modulesdir = @MODULESDIR@

TOPDIR = @CONFIGURE_PWD@
CFGDIR = $(DESTDIR)/$(sysconfdir)/netopeer
CC = @CC@
DOXYGEN = @DOXYGEN@
DOXYGEN_CONFIG = Doxyfile
DOXYGEN_DIR = doxygen
RPMBUILD = @RPMBUILD@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_DATA_PRIVATE = @INSTALL@ -m 600
ifeq ($(shell id -u $(USER)),0)
  INSTALL_OWNER = -o @USER@ -g @GROUP@
endif
RPMDIR = RPMBUILD

AGENT = netopeer-agent
AGENT_LIBS = @AGENT_LIBS@
AGENT_SRCS =  src/agent.c \
	src/agent_comm.c \
	@AGENT_SRCS_COMM@
AGENT_HDRS = src/comm.h \
	@AGENT_HDRS_COMM@
AGENT_OBJS = $(AGENT_SRCS:%.c=$(OBJDIR)/%.o)

SERVER = netopeer-server
SERVER_LIBS = @SERVER_LIBS@
SERVER_SRCS =  src/server.c \
	src/server_operations.c \
	src/cfgnetopeer-transapi.c \
	src/netconf-server-transapi.c \
	@SERVER_SRCS_COMM@
SERVER_HDRS = src/comm.h \
	src/server_operations.h \
	@SERVER_HDRS_COMM@
SERVER_MODULES_CONF = config/Netopeer.xml \
	config/NETCONF-server.xml
SERVER_OBJS = $(SERVER_SRCS:%.c=$(OBJDIR)/%.o)

COMMON_SRCS = src/common.c

MANAGER_SRCS = manager/netopeer-manager.in

CONFIGURATOR_SRCS = configurator/setup.py \
	configurator/netopeer-configurator \
	configurator/netopeer/*.py \
	configurator/netopeer/*.py.in

CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
DEFINE = -DBINDIR=\"$(BINDIR)\" -DCFG_DIR=\"$(CFGDIR)\" -DMODULES_CFG_DIR=\"$(DESTDIR)/$(modulesdir)/\" -DVERSION=\"$(VERSION)\"

OBJDIR= .obj
TOOLS = manager/netopeer-manager
PYTOOLS = configurator/netopeer-configurator

CFGS =	config/netopeer-cfgnetopeer.yin \
	config/netopeer-cfgnetopeer-data.rng \
	config/netopeer-cfgnetopeer-gdefs.rng \
	config/netopeer-cfgnetopeer-schematron.xsl \
	config/ietf-netconf-server.yin \
	config/ietf-x509-cert-to-name.yin \
	config/ietf-netconf-server-data.rng \
	config/ietf-netconf-server-gdefs.rng \
	config/ietf-netconf-server-schematron.xsl
	
CFGS_TAR = $(CFGS) \
	config/Netopeer.xml.in \
	config/NETCONF-server.xml.in \
	config/org.liberouter.netopeer.conf.in \
	config/org.liberouter.netopeer.server.service.in \
	config/Netopeer.xml.in \
	stunnel/stunnel.conf.in

H_IN = src/config.h.in

MANPAGES = manager/netopeer-manager.1 \
	netopeer-server.8 \
	netopeer-agent.1 \
	configurator/netopeer-configurator.1

ifneq "@ROFF2HTML@" "no"
MANHTMLS = $(MANPAGES:%=%.html)
endif

MANPATH=$(DESTDIR)$(mandir)/man$(subst .,,$(suffix $(1)))
define MANINSTALL
[ -d $(call MANPATH,$(1)) ] || \
	(mkdir -p $(call MANPATH,$(1)); \
	chmod 755 $(call MANPATH,$(1))); \
$(INSTALL_DATA) $(1) $(call MANPATH,$(1))/;
endef

define PYINSTALL
cd $(dir $(1)); \
python setup.py install; \
cd -;
endef

# Expand ${prefix} in $(1) file and store it as $(2) file
define EXPAND 
sed -e 's|$${prefix}|$(prefix)|g' $(1) > $(2);
endef

all: $(AGENT) $(SERVER) $(MANHTMLS) $(TOOLS)

$(SERVER_MODULES_CONF): $(SERVER_MODULES_CONF:%=%.tmp)
	$(call EXPAND,$(@:%=%.tmp),$@)

%.1.html: %.1
	@ROFF2HTML@ $< > $@

%.8.html: %.8
	@ROFF2HTML@ $< > $@

$(SERVER): $(SERVER_OBJS) $(SERVER_MODULES_CONF)
	@rm -f $@;
	$(CC) $(CFLAGS) $(CPPFLAGS) $(SERVER_OBJS) $(SERVER_LIBS) -o $@;

$(AGENT): $(AGENT_OBJS)
	@rm -f $@;
	$(CC) $(CFLAGS) $(CPPFLAGS) $(AGENT_OBJS) $(AGENT_LIBS) -o $@;

manager/netopeer-manager: manager/netopeer-manager.tmp
	$(call EXPAND,$<,$@)
	chmod +x $@	

$(OBJDIR)/%.o: %.c
	@[ -d $(dir $@) ] || \
		(mkdir -p $(dir $@))
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFINE) -fPIC -c $< -o $@

.PHONY: clean
clean:
	rm -rf $(AGENT) $(SERVER) $(TOOLS) $(OBJDIR) $(MANHTMLS) doxygen/

.PHONY: doc
doc: $(DOXYGEN_CONFIG)
	@if [ -n "$(DOXYGEN)" ]; then \
		(cat $(DOXYGEN_CONFIG); \
		echo 'OUTPUT_DIRECTORY = $(DOXYGEN_DIR)' ) | \
		$(DOXYGEN) - > /dev/null 2>&1; \
	else \
		echo "Missing doxygen"; \
		exit 1; \
	fi

.PHONY: dist
dist: $(NAME).spec tarball rpm

.PHONY: tarball
tarball: $(SERVER_SRCS) $(AGENT_SRCS) $(SERVER_HDRS) $(AGENT_HDRS)
	@rm -rf $(NAME)-$(VERSION);
	@mkdir $(NAME)-$(VERSION);
	@for i in $(SERVER_SRCS) $(AGENT_SRCS) $(COMMON_SRCS) $(SERVER_HDRS) $(AGENT_HDRS) $(CFGS_TAR) $(SERVER_HDRS_TAR) $(H_IN) configure.in configure \
	    Makefile.in VERSION Doxyfile $(NAME).spec.in netopeer.rc.in install-sh $(MANPAGES) config.sub config.guess $(MANAGER_SRCS) $(CONFIGURATOR_SRCS); do \
	    [ -d $(NAME)-$(VERSION)/$$(dirname $$i) ] || (mkdir -p $(NAME)-$(VERSION)/$$(dirname $$i)); \
		cp $$i $(NAME)-$(VERSION)/$$i; \
	done;
	@rm -rf $(RPMDIR)/SOURCES/; \
	mkdir -p $(RPMDIR)/SOURCES/; \
	tar -c -z -f $(RPMDIR)/SOURCES/$(NAME)-$(VERSION)-$(RELEASE).tar.gz \
	    $(NAME)-$(VERSION); \
	rm -rf $(NAME)-$(VERSION);

.PHONY: rpm
rpm: tarball
	mkdir -p $(RPMDIR)/BUILD $(RPMDIR)/RPMS $(RPMDIR)/SRPMS
	$(RPMBUILD) -ba $(NAME).spec \
		--define "_topdir `pwd`/$(RPMDIR)";

.PHONY: cleanall
cleanall: clean distclean

.PHONY: distclean
distclean:
	rm -rf $(RPMDIR)

.PHONY: install
install: $(SERVER) $(AGENT) $(TOOLS) $(PYTOOLS) $(CFGS) $(MANPAGES) $(MANHTMLS)
	$(INSTALL) -d $(DESTDIR)/$(bindir);
	$(INSTALL_PROGRAM) $(SERVER) $(DESTDIR)/$(bindir)/;
	$(INSTALL_PROGRAM) $(AGENT) $(DESTDIR)/$(bindir)/;
	$(INSTALL_PROGRAM) $(TOOLS) $(DESTDIR)/$(bindir)/;
	$(foreach tool,$(PYTOOLS),$(call PYINSTALL,$(tool)))
	if test -n "$(dbusconfdir)"; then \
		$(INSTALL) -d $(DESTDIR)/$(dbusconfdir)/; \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) config/org.liberouter.netopeer.conf $(DESTDIR)/$(dbusconfdir)/; \
	fi
	if test -n "$(dbusservices)"; then \
		$(INSTALL) -d $(DESTDIR)/$(dbusservices)/; \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) config/org.liberouter.netopeer.server.service $(DESTDIR)/$(dbusservices)/; \
	fi
	$(INSTALL) -d $(DESTDIR)/$(modulesdir)/;
	for i in $(SERVER_MODULES_CONF); do \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) $$i $(DESTDIR)/$(modulesdir)/; \
	done
	$(INSTALL) -d $(CFGDIR)/cfgnetopeer/
	for i in $(CFGS); do \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) $$i $(CFGDIR)/cfgnetopeer/; \
	done
	touch $(CFGDIR)/cfgnetopeer/datastore.xml
	touch $(CFGDIR)/cfgnetopeer/datastore-server.xml
	$(INSTALL) -d $(DESTDIR)/$(sysconfdir)/init.d/;
	$(INSTALL_PROGRAM) -m 755 netopeer.rc $(DESTDIR)/$(sysconfdir)/init.d/
	@if test ! -f $(CFGDIR)/sshd_config; then \
		if test -f /etc/ssh/sshd_config; then \
			grep -Ev "^Subsystem|^Port" /etc/ssh/sshd_config > $(CFGDIR)/sshd_config; \
		else \
			touch $(CFGDIR)/sshd_config; \
		fi; \
		echo "" >> $(CFGDIR)/sshd_config; \
		echo "# NETCONF SSH Subsystem settings" >> $(CFGDIR)/sshd_config; \
		echo "Subsystem netconf $(DESTDIR)/$(bindir)/$(AGENT)" >> $(CFGDIR)/sshd_config; \
	fi
	chmod 600 $(CFGDIR)/sshd_config;
	if test -n "$(INSTALL_OWNER)"; then \
		chown @USER@ $(CFGDIR)/sshd_config; \
	fi
	@echo "NOTE: Check $(CFGDIR)/sshd_config for NETCONF SSH server configuration.";
	@if test -n "@TLS@"; then \
		awk '{sub(/{CFGDIR}/, "'$(CFGDIR)'/stunnel"); print}' config/stunnel_config.in > config/stunnel_config; \
		$(INSTALL) -d $(CFGDIR)/stunnel/; \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) config/stunnel_config $(CFGDIR)/; \
		rm -rf config/stunnel_config; \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) stunnel/server.* $(CFGDIR)/stunnel; \
		$(INSTALL_DATA_PRIVATE) $(INSTALL_OWNER) stunnel/rootCA.* $(CFGDIR)/stunnel; \
		echo "NOTE: Check $(CFGDIR)/stunnel_config for NETCONF SSH server configuration."; \
	fi
	$(foreach man,$(MANPAGES),$(call MANINSTALL,$(man)))
	if test -n "$(MANHTMLS)"; then \
		[ -d $(DESTDIR)/$(datarootdir)/netopeer/ ] || \
			(mkdir -p $(DESTDIR)/$(datarootdir)/netopeer/; chmod 755 $(DESTDIR)/$(datarootdir)/netopeer/); \
	fi
	$(foreach man,$(MANHTMLS),$(INSTALL_DATA) $(man) $(DESTDIR)/$(datarootdir)/netopeer/;)


